/*
 * @Author: zxh
 * @Email: 1271027008@qq.com
 * @Date: 2020-02-26 09:23:40
 * @Descripttion: 库房批次预留
 */
import { Component, OnInit } from '@angular/core';
import { BatchreservedListService } from './batchreserved-list.service'
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { resolve } from 'url';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { HCommonService } from 'src/app/h-common/h-common.service';
@Component({
  selector: 'app-batchreserved-list',
  templateUrl: './batchreserved-list.component.html',
  styleUrls: ['./batchreserved-list.component.scss']
})
export class BatchreservedListComponent implements OnInit {

  power:any
  modalDomBox : any

  constructor(
    private BatchreservedListService: BatchreservedListService,
    private YLForm: FormBuilder,
    private message: NzMessageService,
    private nzModal: NzModalService,
    private HCommonService:HCommonService
  ) { }

  ngOnInit() {
    this.modalDomBox = document.querySelector("#modal_box");
    /* 获取权限 */
    this.power = this.HCommonService.getRoles("batchReserve");
    console.log( this.power );

    this.getKFList()
    this.getYLList()
    this.getUser()
    this.ylForm = this.YLForm.group({
      warehouseId: ['', [Validators.required]],
      warehouseAreaId: ['', [Validators.required]],
      materialId: ['', [Validators.required]],
      stockBatchId: ['', [Validators.required]],
      reserveNum: ['', [Validators.required]],
      reserveReason: [''],
      isReserve: [false],
      barcodeBatch: [''],
    })
  }
  pageSize: number = 10
  KFList = []
  KQList = []
  kfID: string = '';
  kqID: string = '';
  userList = [];
  selectedUserId:string = ''
  // 获取库房列表
  getKFList() {
    this.BatchreservedListService.getAllKF().subscribe((res) => {
      this.KFList = res
    })
  }
  selectedKF(data) {
    this.kfID = data
    this.kqID = ''
    this.getKQList()
    this.getYLList('search')
  }
  selectedKQ(data) {
    this.kqID = data
    this.getYLList('search')
  }
  getKQList() {
    let kfId = this.kfID
    for (let i = 0; i < this.KFList.length; i++) {
      if (this.KFList[i].id == kfId) {
        this.KQList = this.KFList[i].warehouseAreas
      }
    }
  }
  getUser(){
    let _data = {
      page:1,
      pageSize:100000000
    }
    this.BatchreservedListService.getUserList(_data).subscribe(res=>{
      this.userList = res.content
    })
  }
  selectedUser(id){
    this.selectedUserId = id
    this.getYLList('search')
  }

  materialCode: string = ''
  materialName: string = ''
  operateCode: string = ''

  isAllDisplayDataChecked = false;
  isIndeterminate = false;
  listOfDisplayData = [];
  listOfAllData = [];
  mapOfCheckedId: { [key: string]: boolean } = {};
  numberOfChecked = 0;
  curPage: number = 1
  totalSize: number = 0
  currentPageDataChange($event): void {
    this.listOfDisplayData = $event;
    this.refreshStatus();
  }
  refreshStatus(): void {
    this.isAllDisplayDataChecked = this.listOfDisplayData
      .every(item => this.mapOfCheckedId[item.id]);
    if (this.listOfDisplayData.length == 0) {
      this.isAllDisplayDataChecked = false
    }
    this.isIndeterminate =
      this.listOfDisplayData.some(item => this.mapOfCheckedId[item.id]) &&
      !this.isAllDisplayDataChecked;
    this.numberOfChecked = this.listOfAllData.filter(item => this.mapOfCheckedId[item.id]).length;
    // 删除预留id
    let newList = []
    let ids = Object.keys(this.mapOfCheckedId)
    for (let i = 0; i < ids.length; i++) {
      if (this.mapOfCheckedId[ids[i]]) {
        newList.push(ids[i])
      }
    }
    this.deleteYLList = newList
  }
  checkAll(value: boolean): void {
    this.listOfDisplayData.forEach(item => (this.mapOfCheckedId[item.id] = value));
    this.refreshStatus();
  }
  clickPage(index) {
    this.curPage = index
    this.getYLList()
  }
  // 获取 预留 列表
  getYLList(type?) {
    if (type == 'search') {
      this.curPage = 1
    }
    return new Promise((resolve, reject) => {
      try {
        let _data = {
          createBy: this.selectedUserId,
          materialCode: this.materialCode,
          materialName: this.materialName,
          warehouseAreaId: this.kqID,
          warehouseId: this.kfID,
          page: this.curPage - 1,
          size: this.pageSize
        }
        this.BatchreservedListService.getylList(_data).subscribe(res => {
          this.listOfAllData = res.content
          this.totalSize = res.totalElements
        })
        resolve()
      } catch (e) {
        reject(e)
      }
    })
  }

  isShowModal: boolean = false
  modalType: number = 1
  ylForm: FormGroup
  modalKQ = []
  ylId: number
  deleteYLList = [];
  editId: number
  curMetrialCode:string = ''
  curMetrialName: string = ''
  curMetrialId: string = ''
  selectedKfId: number
  selectedKqId:number
  materialList = []
  pcList = []
  showModal(type) {
    this.modalType = type
    this.isShowModal = true
    if (type == 2) {
      let data = this.listOfAllData.filter(item => {
        return item.id == this.deleteYLList[0]
      })[0]
      this.editId = data.id
      this.curMetrialName = data.materialName
      this.curMetrialCode = data.materialCode;
      this.ylForm.patchValue({
        ...data
      })
    } else {
      this.curMetrialName = ''
      this.ylForm.reset()
    }
  }
  handleCancel() {
    this.isShowModal = false
  }
  selectKfModal(kfId) {
    this.selectedKfId = kfId
    this.ylForm.patchValue({
      warehouseAreaId:'',
      materialId:''
    })
    this.curMetrialName = ''
    for (let i = 0; i < this.KFList.length; i++) {
      if (this.KFList[i].id == kfId) {
        this.modalKQ = this.KFList[i].warehouseAreas
      }
    }
  }
  selectKqModal(kqId) {
    if (kqId) {
      this.selectedKqId = kqId
      let _data = {
        warehouseId: this.selectedKfId,
        warehouseAreaId: kqId
      }
      this.BatchreservedListService.getMaterialCode(_data).subscribe(res => {
        this.materialList = res
      })
    }
  }
  selectWlModal(wlCode){
    this.materialList.forEach(item => {
      if(wlCode==item.materialCode){
        this.curMetrialName = item.materialName
        this.curMetrialId = item.materialId
      }
    });
    this.ylForm.patchValue({
      stockBatchId:''
    })
    if(wlCode){
      this.getPCList(wlCode)
    }
  }
  getPCList(wlCode){
    let _data = {
      warehouseId:this.selectedKfId,
      warehouseAreaId:this.selectedKqId,
      materialCode:wlCode
    }
    this.BatchreservedListService.getPCList(_data).subscribe((res:any)=>{
      this.pcList = res
    })
  }
  submitForm() {
    for (const i in this.ylForm.controls) {
      this.ylForm.controls[i].markAsDirty();
      this.ylForm.controls[i].updateValueAndValidity();
    }
    if (this.modalType == 1) {
      let _value = this.ylForm.value;
      this.addYL(_value)
    }else if(this.modalType == 2){
      let _value = this.ylForm.value;
      this.editYL(_value)
    }
  }
  addYL(data) {
    let _data = {
      stockBatchId:data.stockBatchId,
      barcodeBatch:data.barcodeBatch,
      reserveReason:data.reserveReason,
      reserveNum:data.reserveNum,
      isReserve: true
    }
    this.BatchreservedListService.addyl(_data).subscribe(res => {
      this.isShowModal = false
      this.getYLList()
    })
  }
  editYL(data) {
    let _data = {
      id:this.editId,
      stockBatchId:data.stockBatchId,
      barcodeBatch:data.barcodeBatch,
      reserveReason:data.reserveReason,
      reserveNum:data.reserveNum,
      isReserve: data.isReserve
    }
    this.BatchreservedListService.edityl(_data).subscribe(res => {
      this.isShowModal = false
      this.getYLList()
    })
  }
  cancelYL() {
    this.nzModal.confirm({
      nzTitle: '确定要取消预留吗？',
      nzContent: '',
      nzOkText: '确认',
      nzCancelText: '取消',
      nzOnOk: () => {
        let _data = this.deleteYLList
        this.BatchreservedListService.cancelyl(_data).subscribe(res => {
          this.deleteYLList = []
          this.mapOfCheckedId = {}
          this.getYLList()
        })
      }
    })
  }

}